Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Here are the steps to follow: 1. Open the FancyCmdButton designer, and then open its Code window. 2. Select Add Procedure from the Tools menu to display the Insert Procedure dialog box. 3. Specify the procedure name Caption, and select the Property and Public options. Then click on OK. 4. Visual Basic will create skeletons of the Let and Get procedures for you, displaying them in the Code Editing window. Add the code shown in Listing 17.6. Be sure to change the type of the Get procedure from Variant to String. Make the same change for the type of the argument to the Let procedure. Remember that the Get procedure’s return type must be the same as the type of the Let procedure’s argument. Listing 17.6 Get and Let procedures for the Caption property.
    Public Property Get Caption() As String
            Caption = lblButton.Caption
    End Property

    Public Property Let Caption(ByVal vNewValue As String)
        lblButton.Caption = vNewValue
    End Property
Once you have added this code, close the ActiveX designer to put the FancyButtonControl in run mode. Display the test form with the FancyCmdButton on it and click on the button to select it. Look in the Properties window (refer to Figure 17.8), and you’ll see that the control’s property list now includes a Caption property—the one you just defined—in addition to the UserControl’s default properties. If you change this property during design, the text you specify will be displayed on the FancyCmdButton when the test project runs. You could also set the property in code. To try this out, place the following line of code in the test project’s Form_Load procedure:
    FCB1.Caption = “Click Me”
You’ll see that the button displays “Click Me” when the program runs. Figure 17.8 ActiveX control properties that you define are listed in the Visual Basic Properties window along with the default properties. ### Adding A Property Page To The Control You have already seen that the ActiveX control properties you define are automatically displayed in the Visual Basic Properties window. You also have the option of connecting a property page to the control. A property page is simply a different method of displaying and accessing the control’s properties. Each property page you define will become a separate tab in the object’s Properties dialog box. You must design the page, which is done in much the same way as designing a Visual Basic form. Visual Basic takes care of all the details of displaying the tabs, and managing the OK, Cancel, and Apply buttons. Property pages are useful when several properties interact in a complex fashion. You can design the property page so that related properties are grouped together, making it easier for the user to set them properly. Property pages are useful for controls that you plan to distribute internationally, because the captions on the property page can easily be changed to suit different language requirements. Finally, property pages permit controls to be used with development tools that don’t have Properties windows. To add a property page to the FancyCmdButton, click on AXCtrlDemo in the Project window to make the control project current. Then, select Add Property Page from the Project menu. In the next dialog box, select the Property Page icon. (If you like, you can explore the other option—the Visual Basic Property Page wizard—on your own.) Visual Basic adds a property page to the project, as shown in Figure 17.9. The Property Page form is displayed, and its properties are listed in the Properties window. Note that the Visual Basic title bar indicates that the Property Page designer is active by displaying [PropertyPage1 (PropertyPage)]. Note also that a Property Page entry has been added to the listing in the Project window. Figure 17.9 After adding a property page, a blank property page is displayed. In the Properties window, change the property page’s Name property to FCBGeneral and the Caption property to General. The caption will be displayed as the tab title in the Properties dialog box; the name identifies it as the FancyCmdButton’s General property tab. Select Save from the File menu and save the property page under the suggested name, which is the same as the Name property you just assigned (FCBGeneral). Visual Basic automatically adds the .PAG extension to property page files. The next task is to design the property page itself, placing controls on it to permit the user to read and set the control’s properties. Because the FancyCmdButton control has only a single property, Caption, this will be a quick task. Designing a property page is essentially the same as designing a regular Visual Basic form: drag and drop controls and so on. Start by placing a Label control on the property page. Set the Label’s Caption property to Caption. Place a Text Box control under the Label, and set its Text property to a blank string and its Name property to txtCaption. At this point, your property page will look like Figure 17.10. The property page interacts with the control it is attached to by using events. Whenever a property page is opened, it receives a SelectionChanged event. It receives the same event if and when the user changes the controls that are selected on the current form (remember, the property page will be used when the user is designing a form and has placed one of your controls on it). Our task is complicated by the fact that the user can select more than one control—it is perfectly possible for a user to place two or more FancyCmdButton controls on a form and select all of them. Because a property page is modeless, the user can change the selected controls while the property page remains open. Figure 17.10 The property page after placing its two controls. Basically, you have two options for dealing with multiple selected controls. The one you use will depend on the nature of the specific property. For some properties, such as ForeColor, it makes sense to permit the user to change the property setting for two or more controls at once. Note that I am using ForeColor as a generic example—it is not a property of our FancyCmdButton control. In contrast, other properties are not appropriate for such batch changes; if multiple controls are selected, you want to disable that property. The Caption property of our demonstration control falls into the latter category. Dealing with the possibility of multiple selected controls is simplified by the SelectedControls collection, which provides a zero-based index list of the control(s) that are currently selected on the form. You can query this collection’s Count property to see if more than one control is selected, then take the appropriate action. For the single property on the FancyCmdButton’s property page, use the code shown in Listing 17.7. This code is placed in the property page’s SelectionChanged event procedure. Listing 17.7 The property page’s SelectionChanged event procedure.
    Private Sub PropertyPage_SelectionChanged()

    ‘ Enable the Text Box for the Caption
    ‘ property only if there is a single
    ‘ control selected.
    If SelectedControls.Count = 1 Then
        txtCaption.Enabled = True
        ‘ Display the current property value on the property page.
        txtCaption.Text = SelectedControls(0).Caption
    Else
        txtCaption.Enabled = False
    End If

    End Sub
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.